package com.lg.desireplayer.db;

import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;
import com.lg.desireplayer.bandlist.MyList;

;
public class MyListMaker {

	public static void buildMyList(int userid) {

		String strId = null;
		String strName = null;
		String strTitle = null;
		String strDescr = null;
		String strImage = null;
		String strMedia = null;
		String strLink = null;
		String strLikecnt = null;
		int startCnt = 0;
		int endCnt = 0;

		try {
			URL url = new URL(
					"http://192.168.0.131:8080/com.lg.project.mylist.cmd.MyListRetrieveCmd.vcc?userid="
							+ userid);

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(url.openStream(), "utf-8");

			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if (parser.getName().equals("id")) {
						strId = parser.nextText();
						startCnt += 1;
					} else if (parser.getName().equals("name")) {
						strName = parser.nextText();
						Log.i("URL", strName);
					} else if (parser.getName().equals("title")) {
						strTitle = parser.nextText();
						Log.i("URL", strTitle);
					} else if (parser.getName().equals("descr")) {
						strDescr = parser.nextText();
						Log.i("URL", strDescr);
					} else if (parser.getName().equals("image")) {
						strImage = parser.nextText();
						Log.i("URL", strImage);
					} else if (parser.getName().equals("media")) {
						strMedia = parser.nextText();
						Log.i("URL", strMedia);
					} else if (parser.getName().equals("link")) {
						strLink = parser.nextText();
						Log.i("URL", strLink);
					} else if (parser.getName().equals("likecnt")) {
						strLikecnt = parser.nextText();
						Log.i("URL", strLikecnt);
					}

					break;
				case XmlPullParser.END_TAG:
					endCnt += 1;
					if (startCnt == endCnt) {
						MyList.insertBand(Integer.parseInt(strId), strName,
								strTitle, strDescr, strImage, strMedia,
								strLink, Integer.parseInt(strLikecnt));
					}
					break;
				}
				eventType = parser.next();
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
